
package rule

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/rule"
    ruleReq "github.com/flipped-aurora/gin-vue-admin/server/model/rule/request"
)

type RulesService struct {}
// CreateRules 创建rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService) CreateRules(rules *rule.Rules) (err error) {
	err = global.GVA_DB.Create(rules).Error
	return err
}

// DeleteRules 删除rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService)DeleteRules(id string) (err error) {
	err = global.GVA_DB.Delete(&rule.Rules{},"id = ?",id).Error
	return err
}

// DeleteRulesByIds 批量删除rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService)DeleteRulesByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]rule.Rules{},"id in ?",ids).Error
	return err
}

// UpdateRules 更新rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService)UpdateRules(rules rule.Rules) (err error) {
	err = global.GVA_DB.Model(&rule.Rules{}).Where("id = ?",rules.Id).Updates(&rules).Error
	return err
}

// GetRules 根据id获取rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService)GetRules(id string) (rules rule.Rules, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&rules).Error
	return
}
// GetRulesInfoList 分页获取rules表记录
// Author [yourname](https://github.com/yourname)
func (rulesService *RulesService)GetRulesInfoList(info ruleReq.RulesSearch) (list []rule.Rules, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&rule.Rules{})
    var ruless []rule.Rules
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&ruless).Error
	return  ruless, total, err
}
func (rulesService *RulesService)GetRulesPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
